/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski   *
 *   g.latarowski@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#ifndef ROIEDITOR_H
#define ROIEDITOR_H

// Qt
#include <QtGui>
#include <QGraphicsPixmapItem>
#include <QDebug>

// lib
#include "analyzer.h"
#include "autopackets.h"
#include "gxmlelement.h"

// local
#include "marker.h"
#include "roi.h"


// ---------------------- GRoiEditor{} ---------------------------------------
/// \brief Represents a scene used in for setting ROI and markers regions.
///
/// The class renders pixmap on its bakcgrounds and provied usefull
/// items to setup roi and markes regions.
// ---------------------------------------------------------------------------
class GRoiEditor : public QGraphicsScene, public GXmlDocument
{
	Q_OBJECT

public:
	// ====================================================
	// constructors 
	
	/// \brief Creates RoiEditor.
	///
	/// Passes \a pParent to QGraphicsScene constructor, zeroses private
	/// pointers and setups scene. 
	GRoiEditor( QObject * pParent = 0 );

	/// \brief Destroys \e GRoiEditor object.
	///
	/// Destroys all GraphicsItem that are on the scene.
	virtual ~GRoiEditor();

	
	// ====================================================
	// xml element

	virtual QDomElement toXml( QDomDocument & doc )const;
	
	virtual void fromXml( const QDomElement & element );

	virtual QString documentType()const { return QString( "RoiEditor" ); }

	void readAreasDescription( const QDomElement & element );

	
	// ====================================================
	// background pixmap 

	/// \brief Returns backgroung pixmap.
	QPixmap pixmap()const { return _pixmap; }


	// ====================================================
	// roi and markers items manipulation 

	/// \brief Set markers and quadrangled at defaults.
	///
	/// Method creates two markers and quadrangled roi item position at
	/// default. If they were created previously sets them at default.
	/// Does nothing if pixmap wasn't set.
	void setDefaultRegions();
	
	/// \brief Sets roi nodes to \a nodes 
	void setRoiNodes( const QPolygonF & nodes );

	/// \brief Sets start marker.
	///
	/// Start markers starting point is set to \a pos1 and end point to \a pos2.
	void setStartMarkerHooks( const QPointF & pos1, const QPointF & pos2 );

	/// \brief Sets end marker.
	///
	/// End markers starting point is set to \a pos1 and end point to \a pos2.
	void setEndMarkerHooks( const QPointF & pos1, const QPointF & pos2 );
	
	/// \brief Sets shutter marker.
	///
	/// Shutter markers starting point is set to \a pos1 and end point to \a pos2.
	void setShutterMarkerHooks( const QPointF & pos1, const QPointF & pos2 );

	/// \brief Returns roi nodes.
	QPolygonF roiNodes()const;

	/// \brief Returns start markers starting and ending points.
	///
	/// Starting point is at QPolygonF[0] and ending point is at QPolygonF[1].
	QPolygonF startMarkerHooks()const;
	
	/// \brief Returns end markers starting and ending points.
	///
	/// Starting point is at QPolygonF[0] and ending point is at QPolygonF[1].
	QPolygonF endMarkerHooks()const;
	
	/// \brief Returns shutter markers starting and ending points.
	///
	/// Starting point is at QPolygonF[0] and ending point is at QPolygonF[1].
	QPolygonF shutterMarkerHooks()const;


	// ====================================================
	// analyzer pimage_t creation 

	/// \brief Returns markers regions image for \a Analyzer.
	///
	/// Creates a pimage_t structure and fills it with proper values based
	/// on markers position. pimage_t is an indexed color picture.
	/// Start markers region is represented by 0 value.
	/// End markers region is represented by 2 value.
	/// Clear region is represented by 1 value. If markers intersects,
	/// common region is also 1 valued.
	pimage_t markersImage()const;
	
	/// \brief Returns shutter markers regions image for shutter releasing.
	///
	/// Creates a pimage_t structure and fills it with proper values based
	/// on shutter marker position. pimage_t is an indexed color picture.
	/// Shutter marker region is represented by 1 value.
	/// Clear region is represented by 0 value.
	pimage_t shutterImage()const;

	/// \brief Returns roi image for \a Analyzer.
	///
	/// Creates a pimage_t image structure and fill it with proper values based
	/// on roi nodes positions. pimage-T is an indexed color picture.
	/// No interested region is 0 valued and Region Of Interest is 1 valued.
	pimage_t roiImage()const;

	
	// ====================================================
	// scene specific

	/// \brief Shows/hides roi and markers.
	void setToolsVisible( bool visible );

	void setRoiToolVisible( bool visible );
	void setStartMarkerVisible( bool visible );
	void setEndMarkerVisible( bool visible );
	void setShutterMarkerVisible( bool visible );
	
	bool isRoiToolVisible()const;
	bool isStartMarkerVisible()const;
	bool isEndMarkerVisible()const;
	bool isShutterMarkerVisible()const;


	/// \brief Clears widget from any controls and backgrounds.
	void clear();
	
signals:
	void markersChanged();
	void shutterChanged();
	void roiChanged();

	
public slots:
	/// \brief Renders objects on the scene.
	void updateObjects( QList<objectCoordinates> & );
	
	/// \brief Set background _pixmap to \a pixmap.
	///
	/// If \a pixmap size is differenet than actual sceneRect then
	/// sets new sceneRect and setups markers add roi to defaults.
	void updatePixmap( const QPixmap & pixmap );
	
	void setPixmap( const QPixmap & pixmap );

	void setSize( const QSize & size );
	
	void showAccelExceeded();
	void hideAccelExceeded();

	void showAlarm();
	void hideAlarm();
	
protected:
	/// \brief Draws background \e _pixmap in the scenes rect.
	virtual void drawBackground( QPainter * pPainter, const QRectF &rect );
	
private:
	QPixmap   _pixmap;         ///< Background _pixmap
	QSize     _baseSize;
	GMarker * _pStartMarker;   ///< Start marker item
	GMarker * _pEndMarker;     ///< End marker item
	GMarker * _pShutterMarker; ///< Shutter marker item
	GRoi    * _pRoi;           ///< ROI item

	QGraphicsPixmapItem * _pAccelExccededIcon;
	QGraphicsPixmapItem * _pAlarmIcon;
	
	QList< QGraphicsItem * > _tools;
	

	QList< QGraphicsRectItem * >       _objectRects;
	QList< QGraphicsPathItem * >       _objectBarycentrums;
	QList< QGraphicsSimpleTextItem * > _objectDescriptions;
	QList< QGraphicsPathItem * >       _objectTrace0;
	QList< QGraphicsPathItem * >       _objectTrace1;
	QList< QGraphicsPathItem * >       _objectTrace2;

	QPainterPath processTrace( objectCoordinates coordinates, qint16 wantedZone );
	
}; // class !GRoiEditor
// ---------------------------------------------------------------------------


#endif // !ROIEDITOR_H

// eof
